package com.shopping.runbackground.user.controller;

import com.shopping.entity.Result;
import com.shopping.runbackground.user.pojo.Sysadmin;
import com.shopping.runbackground.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 *
 */

@RestController
@RequestMapping("/user")
public class UserContoller {

    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public Result login(Sysadmin sysadmin){
        try {
            Sysadmin sysadmin1 = userService.login(sysadmin);
            if (sysadmin1!=null){
                if (!"停用".equals(sysadmin1.getState())){
                    return new Result(true,20000,"登录成功");
                }
                return new Result(false,20001,"账号已停用");
            }else {
                return new Result(false,20001,"账号或密码错误");
            }
        }catch (Exception e){
            return new Result(false,20001,"登录失败");
        }

    }

    //条件查询用户数据
    @GetMapping("/findUsers")
    public Result findUsers(Sysadmin sysadmin){
        try {
            List<Sysadmin> sysadminList=userService.findUsers(sysadmin);
            return new Result(true,20000,"查询成功",sysadminList);
        }catch (Exception e){
            return new Result(false,20001,"查询失败");
        }
    }

    //编辑回显用户数据
    @GetMapping("/findUser")
    public Result findUser(String username){
        try {
            Sysadmin sysadmin=userService.findUserByusername(username);
            return new Result(true,20000,"查询成功",sysadmin);
        }catch (Exception e){
            return new Result(false,20001,"查询失败");
        }
    }


    //添加用户
    @PostMapping("/addUser")
    public Result addUser(@RequestBody Sysadmin sysadmin){
        try {
            userService.addUser(sysadmin);
            return new Result(true,20000,"添加成功");
        }catch (Exception e){
            return new Result(false,20001,"添加失败");
        }
    }
}
